package com.cxs.drawpointdemo.slice;

import com.cxs.drawpointdemo.ResourceTable;
import com.cxs.drawpointdemo.constant.Common;
import com.cxs.drawpointdemo.data.Question;
import com.cxs.drawpointdemo.point.DrawPoint;
import com.cxs.drawpointdemo.point.MyPoint;
import com.cxs.drawpointdemo.util.DialogUtils;
import com.cxs.drawpointdemo.util.KvStoreUtils;
import com.cxs.drawpointdemo.util.ZSONUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.service.WindowManager;
import ohos.data.distributed.common.ChangeNotification;
import ohos.data.distributed.common.Entry;
import ohos.data.distributed.common.KvStoreObserver;
import ohos.data.distributed.common.SubscribeType;
import ohos.data.distributed.user.SingleKvStore;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;

public class GuessAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00102, "GuessAbilitySlice");

    private SingleKvStore kvStore = null;
    private MyKvStoreObserver observer;
    private DrawPoint drawPoint;
    private Text textQuestionInfo, textContent;
    private TextField tfInput;
    private Button btnSend;
    private Question question = new Question();
    private static String congratulation = "你猜对了";
    private static int durationTime = 3000;

    class MyKvStoreObserver implements KvStoreObserver {
        @Override
        public void onChange(ChangeNotification changeNotification) {
            HiLog.info(LABEL, "MyKvStoreObserver");
            List<Entry> insertEntries = changeNotification.getInsertEntries();
            List<Entry> updateEntries = changeNotification.getUpdateEntries();
            insertEntries.forEach(entry -> changeNotifi(entry));
            updateEntries.forEach(entry -> changeNotifi(entry));
        }
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_guess);

        initDB();
        initComponent();
    }

    private void initComponent() {
        getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);

        drawPoint = (DrawPoint)findComponentById(ResourceTable.Id_drawpoint);
        drawPoint.setGuessPlay(true);

        textQuestionInfo = (Text) findComponentById(ResourceTable.Id_text_guess_question_info);
        textContent = (Text)findComponentById(ResourceTable.Id_text_guess_message_content);
        tfInput = (TextField) findComponentById(ResourceTable.Id_tf_input_message);

        btnSend = (Button) findComponentById(ResourceTable.Id_btn_guess_send);
        btnSend.setClickedListener(c -> {
            if (tfInput.getText().equals(question.getTitle())) {
                sendMessage(tfInput.getText() + " " + congratulation);
                kvStore.putInt(Common.RESULT_KEY, Common.SUCCESSFUL);
                DialogUtils.showTip(getContext(), congratulation, durationTime);
            } else {
                sendMessage(tfInput.getText());
                kvStore.putString(Common.INPUT_MESSAGE_KEY, tfInput.getText());
                tfInput.setText("");
            }
        });
    }

    private void initDB() {
        kvStore = KvStoreUtils.getKvStore();
        observer = new MyKvStoreObserver();
        kvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_REMOTE, observer);
    }

    private void changeNotifi(Entry entry) {
        String key = entry.getKey();
        switch (key) {
            case Common.POINT_LIST_KEY:
                // 坐标集合
                String str = entry.getValue().getString();
                List<MyPoint> myPoints = ZSONUtils.toList(str);
                getUITaskDispatcher().asyncDispatch(() -> {
                    drawPoint.setPointList(myPoints);
                });
                break;
            case Common.INPUT_MESSAGE_KEY:
                // 发送消息
                sendMessage(entry.getValue().getString());
                break;
            case Common.QUESTION_TITLE_KEY:
                // 题目
                question.setTitle(entry.getValue().getString());
                break;
            case Common.QUESTION_DESCRIBE_KEY:
                // 题目的描述
                question.setDescribe(entry.getValue().getString());
                getUITaskDispatcher().asyncDispatch(() -> {
                    textQuestionInfo.setText(question.getDescribe());
                });
                break;
            case Common.QUESTION_FIRST_TIP_KEY:
                // 提示
                question.setTip1(entry.getValue().getString());
                sendMessage(question.getTip1());
                break;
            case Common.QUESTION_SECOND_TIP_KEY:
                // 提示
                question.setTip2(entry.getValue().getString());
                sendMessage(question.getTip2());
                break;
            case Common.RESELECT_QUESTION_KEY:
                question.setTitle(entry.getValue().getString());
                sendMessage("已重新选择词汇！");
                break;
            case Common.RESTART_KEY:
                question.setTitle(entry.getValue().getString());
                sendMessage("重新开始");
                break;
        }
    }

    private void sendMessage(String str) {
        getUITaskDispatcher().asyncDispatch(() -> {
            textContent.append(str + "\n");
        });
    }
}
